package com.engine.xfjt.duss.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-08-12
 * @Description: ${description}
 * @ModifyLog:
 **/

public class XfjtUtil extends BaseBean {
    /**
     * 获取考试考题正确答案
     */
    public JSONObject getRightAnswer(String id) {
        RecordSet rs = new RecordSet();
        JSONObject jsonData = new JSONObject();
        JSONArray childList = new JSONArray();
        rs.executeQuery("select xx,px from uf_ktsz_dt1 where mainid=? and zqda=0 order by px", id);
        while (rs.next()) {
            JSONObject json = new JSONObject();
            json.put("xxid",Util.null2String(rs.getString("px")));
            json.put("xxnr",Util.null2String(rs.getString("xx")));
            childList.add(json);
        }
        jsonData.put("zqda",childList);
        return jsonData;
    }

    /**
     * 获取考试倒计时
     */
    public String getCountDown(String requestid) {
        RecordSet rs = new RecordSet();
        String djsjssj = getFieldValByname("formtable_main_286","djsjssj", requestid);
        String kssc = getFieldValByname("formtable_main_286","kssc", requestid);
        if ("".equals(djsjssj)) {
            addKsjssj(kssc, requestid);
        }
        String sql = "select datediff(second,convert(varchar(100),getdate(),120),djsjssj) from formtable_main_286 where requestid=?";
        rs.executeQuery(sql, requestid);
        rs.next();
        String djs = Util.null2String(rs.getString(1));
        return secondConvertHourMinSecond(Long.valueOf(djs));
    }

    /**
     * 获取考试倒计时时长
     */
    public String getformtKssc(String requestid) {
        String formtCountDown = "";
        int kssc = Util.getIntValue(getFieldValByname("formtable_main_295","kssc", requestid));
        if (kssc > 0) {
            formtCountDown = secondConvertHourMinSecond((long) (kssc * 60));
        }
        return formtCountDown;
    }

    /**
     * 获取考试题目信息
     */
    public JSONObject getKstmxx(String id) {
        RecordSet rs = new RecordSet();
        JSONObject jsonData = new JSONObject();
        rs.executeQuery("select id,tm,ktlx from uf_ktsz where id=?", id);
        rs.next();
        String tmname = Util.null2String(rs.getString("tm"));
        jsonData.put("tmname", tmname);
        String ktlx = Util.null2String(rs.getString("ktlx"));
        String mainid = Util.null2String(rs.getString("id"));
        JSONArray childList = new JSONArray();
        rs.executeQuery("select xx,px from uf_ktsz_dt1 where mainid=? order by px", mainid);
        while (rs.next()) {
            JSONObject json = new JSONObject();
            String xx = Util.null2String(rs.getString("xx"));
            int px = Util.getIntValue(rs.getString("px"));
            String xxmc = "";
            String xxnr = "";
            if ("0".equals(ktlx) || "1".equals(ktlx)) {
                xxmc = getKsxx(px);
                xxnr = xx;
            }
            if ("2".equals(ktlx)) {
                xxmc = xx;
            }
            json.put("xxid", px);
            json.put("xxnr", xxnr);
            json.put("xxmc", xxmc);
            childList.add(json);
        }
        jsonData.put("child", childList);

        return jsonData;
    }

    /**
     * 转换考试选项
     */
    public String getKsxx(int val) {
        String[] strs = new String[]{"A.", "B.", "C.", "D.", "E.", "F.", "G.", "H.", "I.", "J.", "K.", "L.", "M.", "N."};
        return strs[val - 1];
    }
    /**
     * 转换考试选项
     */
    public String getPdtxx(String id,String xh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_ktsz where id=?", id);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));
        rs.executeQuery("select xx from uf_ktsz_dt3 where mainid=? and px=?", mainid,xh);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 通用字段查询
     */
    public String getFieldValByname(String tableName,String filename, String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select " + filename + " from "+tableName+" where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 新增考试结束时间
     */
    public boolean addKsjssj(String kssc, String requestid) {
        RecordSet rs = new RecordSet();
        String sql = "update formtable_main_286 set djskssj=convert(varchar(100),getdate(),120)," +
                "djsjssj=convert(varchar(100),dateadd(minute," + kssc + ",getdate()),120)" +
                " where requestid=?";
        return rs.executeUpdate(sql, requestid);
    }

    /**
     * 通用的插入操作
     */
    public boolean insertData(String tablename, Map<String, Object> map) {
        String insertsql = "insert into " + tablename + "";
        String keyString = "";
        String valueString = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (map.get(key) == null || "".equals(map.get(key).toString())) {
                continue;
            }
            keyString += key + ",";
            valueString += "'" + map.get(key).toString() + "',";
        }
        if (keyString.endsWith(",")) {
            keyString = keyString.substring(0, keyString.length() - 1);
        }
        if (valueString.endsWith(",")) {
            valueString = valueString.substring(0, valueString.length() - 1);
        }
        insertsql += "(" + keyString + ") values(" + valueString + ")";
        RecordSet rs = new RecordSet();
        boolean flag = rs.executeUpdate(insertsql);
        return flag;
    }

    /**
     * 根据formid获取建模表名称
     */
    public String getTabNameByFormid(String formid) {
        RecordSet rs = new RecordSet();
        String tablename = "";
        String sql = "select tablename from WORKFLOW_BILL where id = ?";
        rs.executeQuery(sql, formid);
        if (rs.next()) {
            tablename = Util.null2String(rs.getString("tablename"));
        }
        return tablename;
    }

    /**
     * 根据userid获取人员的部门和分部
     */
    public Map<String, String> getDepartAndSub(String userid) {
        Map<String, String> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentid,subcompanyid1 from hrmresource where id='" + userid + "'");
        rs.next();
        String departmentid = Util.null2String(rs.getString("departmentid"));
        String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
        map.put("departmentid", departmentid);
        map.put("subcompanyid", subcompanyid1);
        return map;
    }

    /**
     * 根据根据requestid插入明细数据
     */
    public int addDetailData(String type, String requestid, String ids) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from formtable_main_37 where requestid=?", requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));
        int count = 0;
        if ("0".equals(type)) {
            rs.executeQuery("select id,wjbt,swh,swrq,swdw,gwlx,dackqx,xglc from uf_swjy where id in (" + ids + ") and dackqx !=0");
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("bt", Util.null2String(rs.getString("wjbt")));
                map.put("wh", Util.null2String(rs.getString("swh")));
                map.put("gwlx", Util.null2String(rs.getString("gwlx")));
                map.put("szdw", Util.null2String(rs.getString("swdw")));
                map.put("wjrq", Util.null2String(rs.getString("swrq")));
                map.put("dackqx", Util.null2String(rs.getString("dackqx")));
                map.put("xgjmsjid", Util.null2String(rs.getString("id")));
                map.put("xglc", Util.null2String(rs.getString("xglc")));
                map.put("mainid", mainid);
                boolean flag = insertData("formtable_main_37_dt1", map);
                if (flag) {
                    count++;
                }
            }
        }
        if ("1".equals(type)) {
            rs.executeQuery("select id,sy,fwh,yfrq,fwdw,gwlx,dackqx,xglc from uf_fwjy where id in (" + ids + ") and dackqx !=0");
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                map.put("bt", Util.null2String(rs.getString("sy")));
                map.put("wh", Util.null2String(rs.getString("fwh")));
                map.put("gwlx", Util.null2String(rs.getString("gwlx")));
                map.put("szdw", Util.null2String(rs.getString("fwdw")));
                map.put("wjrq", Util.null2String(rs.getString("yfrq")));
                map.put("dackqx", Util.null2String(rs.getString("dackqx")));
                map.put("xgjmsjid", Util.null2String(rs.getString("id")));
                map.put("xglc", Util.null2String(rs.getString("xglc")));
                map.put("mainid", mainid);
                boolean flag = insertData("formtable_main_37_dt1", map);
                if (flag) {
                    count++;
                }
            }
        }
        return count;
    }

    /**
     * 根据类型和建模数据ID带入到流程中
     */
    public Map<String, String> getExchangeDocData(String type, String id) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        Map<String, String> map = new HashMap<>();
        if ("0".equals(type)) {
            rs.executeQuery("select gwlx,swwj,wjbt,swh from uf_swtz where id =?", id);
            rs.next();
            map.put("xzxtfw", "");
            map.put("xzxtsw", id);
            map.put("gwlx", Util.null2String(rs.getString("gwlx")));
            map.put("oaxtwj", Util.null2String(rs.getString("swwj")));
            map.put("gwmc", Util.null2String(rs.getString("wjbt")));
            map.put("wh", Util.null2String(rs.getString("swh")));
            bb.writeLog(">>>>>getExchangeDocData>>>map=" + map.toString());
        }

        if ("1".equals(type)) {
            rs.executeQuery("select gwlx,zw,sy,fwh from uf_fwtz where id =?", id);
            rs.next();
            map.put("xzxtfw", id);
            map.put("xzxtsw", "");
            map.put("gwlx", Util.null2String(rs.getString("gwlx")));
            map.put("oaxtwj", Util.null2String(rs.getString("zw")));
            map.put("gwmc", Util.null2String(rs.getString("sy")));
            map.put("wh", Util.null2String(rs.getString("fwh")));
            bb.writeLog(">>>>>getExchangeDocData>>>map=" + map.toString());
        }
        return map;
    }

    /**
     * 将秒转为时分秒格式【01:01:01】
     */
    public String secondConvertHourMinSecond(Long second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }
}
